﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer.App1AppHandler.Spells.Interfaces;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_FrozenFeet : TargetSpell, IInstantCast
    {
        public Spell_FrozenFeet() : base() { }

        public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            //Resume a normal stun with this spell icon.

            Spell_FrozenFeetBuff ffSpell = new Spell_FrozenFeetBuff();
            ffSpell.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
        }

        private void MainCast(CreatureBase castingCreature)
        {
            //TargetCreatureBase.currentMapItem.SpellLayer0.Add(this);
            //MapItems.Add(TargetCreatureBase.currentMapItem);

            List<CreatureBase> creatureBasesToUpdate = new List<CreatureBase>();
            foreach (CreatureBase creaturebase in TargetCreatureBase.currentMapItem.CreaturesBases)
            {
                if (creaturebase.HP > 0 && 
                    (creaturebase.Faction == TargetCreatureBase.Faction || castingCreature.hateManager.GetCreatureHateInfo(creaturebase) != null) && 
                    creaturebase.CanSeeMe(castingCreature))
                {
                    Spell_FrozenFeetBuff buff = new Spell_FrozenFeetBuff();
                    bool resisted = buff.tryResist(spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, creaturebase);
                    if (resisted)
                    {
                        castingCreature.SendMessage(TargetCreatureBase.Name + " resists frozen foot!", "spelldefense");
                        TargetCreatureBase.SendMessage("You resist " + castingCreature.Name + "'s frozen foot!", "spelloffense");
                        continue;
                    }

                    CreatureBase cb = creaturebase;
                    buff.Resume(cb, this.spellIcon, -1, CastingCreatureLevel, CastingCharacterID, CastingCreatureInt, CastingCreatureWis, -1, null);
                    cb.buffManager.UpdateBuffs();

                    creatureBasesToUpdate.Add(cb);

                    if (castingCreature is Character && castingCreature != creaturebase && creaturebase != TargetCreatureBase)
                    {
                        Character character = (Character)castingCreature;

                        Message msg = new Message();
                        msg.CreatureID = character.CreatureID;
                        msg.Type = "spelloffense";
                        msg.Text = "You cast " + this.spellIcon.Name + " on " + creaturebase.Name;

                        character.AddMessage(msg);
                    }
                    if (creaturebase is Character && creaturebase != castingCreature && creaturebase != TargetCreatureBase)
                    {
                        Character character = (Character)creaturebase;

                        Message msg = new Message();
                        msg.CreatureID = character.CreatureID;
                        msg.Type = "spelloffense";
                        msg.Text = castingCreature.Name + " casts " + this.spellIcon.Name + " on you";

                        character.AddMessage(msg);
                    }
                }
            }

            foreach (MapItem mapitem in TargetCreatureBase.currentMapItem.mapItemsWithin8)
            {
                //Play Sound

                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                    {
                        Character character = (Character)creaturebase;

                        //if (Math.Abs(character.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(character.MapY - TargetCreatureBase.MapY) <= 4)
                        //    character.AddMapItem(TargetCreatureBase.currentMapItem);

                        //xml.@name, int(xml.@mapid), int(xml.@mapx), int(xml.@mapy), int(xml.@layer)

                        character.AddAnimation(spellIcon.FieldImageURL, TargetCreatureBase.MapID, TargetCreatureBase.MapX, TargetCreatureBase.MapY, 0);

                        foreach (CreatureBase cb in creatureBasesToUpdate)
                            character.AddCreatureBase(cb);
                    }
                }
            }
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            MainCast(myCreatureBase);
        }


        public void InstantCast(CreatureBase castingCreature, CreatureBase targetCreatureBase, SpellIcon _spellIcon, int castLevel)
        {
            if (InitializeInstantCast(castingCreature, targetCreatureBase, _spellIcon, castLevel, false) == false)
                return;

            MainCast(castingCreature);
        }
    }
}
